﻿' BasecampVB - http://www.tidbitsandtangents.com
' Copyright (c) 2009
' by Rusty S. Swayne
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports TnT.BasecampVB.DAL

Namespace BLL

    Public Class Project
        Inherits BasecampBase

        ''' <summary>
        ''' Constructor
        ''' </summary>
        Public Sub New( _
            ByVal project_id As Integer, _
            ByVal company_id As Integer, _
            ByVal announcement As String, _
            ByVal name As String, _
            ByVal start_page As String, _
            ByVal show_announcement As Boolean, _
            ByVal show_writeboards As Boolean, _
            ByVal status As String, _
            ByVal created_on As DateTime)

            Me.project_id = project_id
            Me.company_id = company_id
            Me.announcement = announcement
            Me.name = name
            Me.start_page = start_page
            Me.show_announcement = show_announcement
            Me.show_writeboards = show_writeboards
            Me.status = status
            Me.created_on = created_on

        End Sub

#Region "Project Methods"


        ''' <summary>
        ''' Returns a List of all projects as List(Of Project) from Basecamp API
        ''' </summary>
        Public Shared Function GetProjects() As List(Of Project)
            Return GetProjectListFromProjectDetailsCollection(BasecampProvider.API.GetProjects())
        End Function

        ''' <summary>
        ''' Returns Project from Basecamp API
        ''' </summary>
        Public Shared Function GetProject(ByVal project_id As Integer) As Project
            Return GetProjectFromProjectDetails(BasecampProvider.API.GetProject(project_id))
        End Function

#End Region

#Region "Messages"

        ''' <summary>
        ''' Creates a Message associated with the currently instantiated project in Basecamp via Basecamp API
        ''' </summary>
        Public Overloads Function CreateMessage(ByVal title As String, ByVal body As String, Optional ByVal is_private As Boolean = False) As Integer
            Return CreateMessage(title, body, String.Empty, is_private)
        End Function

        ''' <summary>
        ''' Creates a Message associated with the currently instantiated project in Basecamp via Basecamp API
        ''' </summary>
        Public Overloads Function CreateMessage(ByVal title As String, ByVal body As String, ByVal extended_body As String, Optional ByVal is_private As Boolean = False) As Integer
            Return CreateMessage(0, title, body, extended_body, is_private)
        End Function

        ''' <summary>
        ''' Creates a Message associated with the currently instantiated project in Basecamp via Basecamp API
        ''' </summary>
        Public Overloads Function CreateMessage(ByVal category_id As Integer, _
            ByVal title As String, ByVal body As String, ByVal extended_body As String, Optional ByVal is_private As Boolean = False) As Integer
            Return CreateMessage(category_id, 0, title, body, extended_body, is_private)
        End Function

        ''' <summary>
        ''' Creates a Message associated with the currently instantiated project in Basecamp via Basecamp API
        ''' </summary>
        Public Overloads Function CreateMessage(ByVal category_id As Integer, ByVal milestone_id As Integer, _
            ByVal title As String, ByVal body As String, ByVal extended_body As String, Optional ByVal is_private As Boolean = False) As Integer
            Return CreatePost(project_id, category_id, milestone_id, title, body, extended_body, is_private, Nothing)
        End Function

        ''' <summary>
        ''' Creates a Message associated with the currently instantiated project in Basecamp via Basecamp API
        ''' </summary>
        Public Overloads Shared Function CreatePost(ByVal project_id As Integer, ByVal category_id As Integer, ByVal milestone_id As Integer, _
            ByVal title As String, ByVal body As String, ByVal extended_body As String, ByVal is_private As Boolean, ByVal notify As List(Of Person)) As Integer
            Dim person_ids As List(Of Integer) = Nothing
            Dim p As Person = Nothing
            If notify IsNot Nothing Then
                person_ids = New List(Of Integer)
                For Each p In notify
                    person_ids.Add(p.person_id)
                Next
            End If
            Return BasecampProvider.API.CreatePost(project_id, category_id, milestone_id, title, body, extended_body, is_private, person_ids)
        End Function

#End Region

#Region "ToDoLists"

        ''' <summary>
        ''' Creates a ToDoList associated with the currently instantiated project in Basecamp via Basecamp API
        ''' </summary>
        Public Function GetToDoLists(ByVal filter As ToDoListType) As List(Of ToDoList)
            Return ToDoList.GetToDoLists(Me.project_id, filter)
        End Function

        ''' <summary>
        ''' Creates a ToDoList associated with the currently instantiated project in Basecamp via Basecamp API
        ''' </summary>
        Public Function CreateToDoList(ByVal milestone_id As Integer, ByVal name As String, ByVal description As String, ByVal tracked As Boolean, ByVal is_private As Boolean, Optional ByVal todo_list_template_id As Integer = 0) As Integer
            Return ToDoList.CreateToDoList(Me.project_id, milestone_id, name, description, tracked, is_private, todo_list_template_id)
        End Function



#End Region

#Region "Private Utility Methods"

        ''' <summary>
        ''' Helper Function to create a List(Of Project) from List(Of ProjectDetails) 
        ''' </summary>
        Private Shared Function GetProjectListFromProjectDetailsCollection(ByVal recordset As List(Of ProjectDetails)) As List(Of Project)
            Dim projects As New List(Of Project)
            Dim record As ProjectDetails = Nothing
            For Each record In recordset
                projects.Add(GetProjectFromProjectDetails(record))
            Next
            Return projects
        End Function

        ''' <summary>
        ''' Helper Function to create a Project from ProjectDetails
        ''' </summary>
        Private Shared Function GetProjectFromProjectDetails(ByVal record As ProjectDetails) As Project
            Return New Project(record.project_id, record.company_id, record.announcement, record.name, record.start_page, record.show_announcement, _
                               record.show_writeboards, record.status, record.created_on)
        End Function

#End Region

#Region "Custom Properties"

        Public ReadOnly Property messages() As List(Of Post)
            Get
                Return Post.GetPosts(Me.project_id)
            End Get
        End Property

        Public ReadOnly Property company() As Company
            Get
                Return company.GetCompany(Me.company_id)
            End Get
        End Property

        Public ReadOnly Property todo_lists() As List(Of ToDoList)
            Get
                Return GetToDoLists(ToDoListType.all)
            End Get
        End Property




#End Region

#Region "Properties"

        Private _project_id As Integer
        Public Property project_id() As Integer
            Get
                Return _project_id
            End Get
            Set(ByVal value As Integer)
                _project_id = value
            End Set
        End Property


        Private _company_id As Integer
        Public Property company_id() As Integer
            Get
                Return _company_id
            End Get
            Set(ByVal value As Integer)
                _company_id = value
            End Set
        End Property


        Private _announcement As String
        Public Property announcement() As String
            Get
                Return _announcement
            End Get
            Set(ByVal value As String)
                _announcement = value
            End Set
        End Property

        Private _name As String
        Public Property name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property

        Private _start_page As String
        Public Property start_page() As String
            Get
                Return _start_page
            End Get
            Set(ByVal value As String)
                _start_page = value
            End Set
        End Property

        Private _show_announcement As Boolean
        Public Property show_announcement() As Boolean
            Get
                Return _show_announcement
            End Get
            Set(ByVal value As Boolean)
                _show_announcement = value
            End Set
        End Property

        Private _show_writeboards As Boolean
        Public Property show_writeboards() As Boolean
            Get
                Return _show_writeboards
            End Get
            Set(ByVal value As Boolean)
                _show_writeboards = value
            End Set
        End Property

        Private _status As String
        Public Property status() As String
            Get
                Return _status
            End Get
            Set(ByVal value As String)
                _status = value
            End Set
        End Property

        Private _created_on As DateTime
        Public Property created_on() As DateTime
            Get
                Return _created_on
            End Get
            Set(ByVal value As DateTime)
                _created_on = value
            End Set
        End Property


#End Region


    End Class

End Namespace